﻿using System;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using Library;
using Library.Models;
using Newtonsoft.Json;

namespace Ecommerce
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("elmah.axd");

            routes.MapRoute(
                "AccountCulture",
                "{culture}/Account/{action}",
                new { controller = "Account", action = UrlParameter.Optional },
                new { culture = @"[a-z]{2}-[a-zA-Z]{2}" }
            );

            routes.MapRoute(
                "Account",
                "Account/{action}",
                new { controller = "Account", action = UrlParameter.Optional }
            );

            routes.MapRoute(
                "CultureAdminProductCategories",
                "{culture}/Admin/product-categories",
                new { controller = "Admin", action = "ProductCategories" },
                new { culture = @"[a-z]{2}-[a-zA-Z]{2}" }
            );

            routes.MapRoute(
                "AdminProductCategories",
                "Admin/product-categories",
                new { controller = "Admin", action = "ProductCategories" }
            );

            routes.MapRoute(
                "CultureAdminPageGroups",
                "{culture}/Admin/page-groups",
                new { controller = "Admin", action = "PageGroups" },
                new { culture = @"[a-z]{2}-[a-zA-Z]{2}" }
            );

            routes.MapRoute(
                "AdminPageGroups",
                "Admin/page-groups",
                new { controller = "Admin", action = "PageGroups" }
            );

            routes.MapRoute(
                "CultureAdminProductGallery",
                "{culture}/Admin/product-gallery",
                new { controller = "Admin", action = "ProductGallery" },
                new { culture = @"[a-z]{2}-[a-zA-Z]{2}" }
            );

            routes.MapRoute(
                "AdminProductGallery",
                "Admin/product-gallery",
                new { controller = "Admin", action = "ProductGallery" }
            );

            routes.MapRoute(
                "CultureAdminPageGallery",
                "{culture}/Admin/page-gallery",
                new { controller = "Admin", action = "PageGallery" },
                new { culture = @"[a-z]{2}-[a-zA-Z]{2}" }
            );

            routes.MapRoute(
                "AdminPageGallery",
                "Admin/page-gallery",
                new { controller = "Admin", action = "PageGallery" }
            );

            routes.MapRoute(
                "CultureAdminProductSpecifications",
                "{culture}/Admin/product-specifications/{id}",
                new { controller = "Admin", action = "ProductSpecifications", id = UrlParameter.Optional },
                new { culture = @"[a-z]{2}-[a-zA-Z]{2}" }
            );

            routes.MapRoute(
                "AdminProductSpecifications",
                "Admin/product-specifications/{id}",
                new { controller = "Admin", action = "ProductSpecifications", id = UrlParameter.Optional }
            );


            routes.MapRoute(
                "CultureAdminURLList",
                "{culture}/Admin/url-list",
                new { controller = "Admin", action = "UrlList" },
                new { culture = @"[a-z]{2}-[a-zA-Z]{2}" }
            );

            routes.MapRoute(
                "AdminURLList",
                "Admin/url-list",
                new { controller = "Admin", action = "UrlList" }
            );

            routes.MapRoute(
                "AdminEditSettings",
                "Admin/settings",
                new { controller = "Admin", action = "EditSettings" }
            );

            routes.MapRoute(
                "CultureAdmin",
                "{culture}/Admin/{action}/{id}",
                new { controller = "Admin", action = "Index", id = UrlParameter.Optional },
                new { culture = @"\w{2,3}(-\w{4})?(-\w{2,3})?" }
            );

            routes.MapRoute(
                "Admin",
                "Admin/{action}/{id}",
                new { controller = "Admin", action = "Index", id = UrlParameter.Optional }
            );

            routes.MapRoute(
                "SharedCulture",
                "{culture}/Shared/{action}",
                new { controller = "Share", action = UrlParameter.Optional },
                new { culture = @"[a-z]{2}-[a-zA-Z]{2}" }
            );

            routes.MapRoute(
                "Shared",
                "Shared/{action}",
                new { controller = "Shared", action = UrlParameter.Optional }
            );

            try
            {
                var dbRoutes = new EcommerceEntities().Routes.Where(r => r.IsDisabled == null || (r.IsDisabled.HasValue && !r.IsDisabled.Value)).OrderBy(r => r.PriorityOrder);
                foreach (var dbRoute in dbRoutes)
                {
                    if (string.IsNullOrEmpty(dbRoute.Constraints))
                    {
                        routes.MapRoute(
                            dbRoute.Name,
                            dbRoute.Url,
                            JsonConvert.DeserializeObject<RouteValueDictionary>(dbRoute.Defaults)
                        );
                    }
                    else
                    {
                        routes.MapRoute(
                            dbRoute.Name,
                            dbRoute.Url,
                            JsonConvert.DeserializeObject<RouteValueDictionary>(dbRoute.Defaults),
                            JsonConvert.DeserializeObject<RouteValueDictionary>(dbRoute.Constraints)
                        );
                    }
                }
            }
            catch (Exception e)
            {
                LogHelper.Write(e);
            }

            routes.MapRoute(
                "HomeCulture",
                "{culture}/{controller}/{action}",
                new { controller = "Home", action = "Index", culture = UrlParameter.Optional }
            );

            routes.MapRoute(
                "Home",
                "{controller}/{action}",
                new { controller = "Home", action = "Index" }
            );
        }
    }
}
